package cn.buk.qms.aop;

import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Enumeration;

@Aspect
@Component
public class LogRequestAspect2 {

  private static final  Logger logger = LoggerFactory.getLogger(LogRequestAspect2.class);
//  private static final Logger logger = LoggerFactory.getLogger(DaoPerformance.class);

  @Pointcut("execution(public * cn.buk.qms.web..*.*(..))")
  public void log2() {

  }

  @Before("log2()")
  public void doBefore(JoinPoint joinPoint) {
//    System.out.println("aop doBefore..");
//    logger.info("aop doBefore.." + joinPoint.toString());
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = attributes.getRequest();

    //url
//    logger.info("url={}",request.getRequestURI());

    //method
//    logger.info("method={}", request.getMethod());

    //ip
//    logger.info("ip={}", request.getRemoteAddr());

    //类方法
//    logger.info("classMethod={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());

    //参数
    Enumeration<String> paramter = request.getParameterNames();
    while (paramter.hasMoreElements()) {
      String str =  paramter.nextElement();
//      logger.info(str + "={}", request.getParameter(str));
    }

  }

  @After("log2()")
  public void doAfter() {
//    logger.info("aop doAfter");
  }
}
